/*
 * Copyright (C) 2011 Lavettacn Xiao (lavettacn@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lavettacn.android.app.sociallessness.engine;

import com.lavettacn.android.app.sociallessness.util.Debug;

import android.os.HandlerThread;
import android.os.Message;

public class Scheduler {

    private static final String TAG = "Scheduler:";

    private HandlerThread mThread = null;

    private SchedulerHandler mHandler = null;

    public Scheduler() {
    }

    public void start() {
        Debug.logD(TAG + "start");
        mThread = new HandlerThread("Scheduler");
        mThread.start();
        mHandler = new SchedulerHandler(mThread.getLooper());
    }

    public void stop() {
        Debug.logD(TAG + "stop");
        mThread.quit();
        mThread = null;
        mHandler = null;
    }

    public final boolean sendMessage(Message msg) {
        Debug.logD(TAG + "sendMessage");
        if (mHandler != null) {
            return mHandler.sendMessage(msg);
        }
        return false;
    }

    public boolean post(Runnable r) {
        Debug.logD(TAG + "post");
        if (mHandler != null) {
            return mHandler.post(r);
        }
        return false;
    }

    public final boolean postAtFrontOfQueue(Runnable r) {
        Debug.logD(TAG + "postAtFrontOfQueue");
        if (mHandler != null) {
            return mHandler.postAtFrontOfQueue(r);
        }
        return false;
    }
}
